package com.oner365.test.socket.keepalive;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Hashtable;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class LongConnectionServer {
  
  private static final int SERVER_PORT = 8884;
  
  private static final Logger LOGGER = LoggerFactory.getLogger(LongConnectionServer.class);
  private static Hashtable<String, Socket> hashtable = new Hashtable<>();

  public static void main(String[] args) throws IOException {

    LOGGER.info("==============SERVER==============");
    ServerSocket serverSocket = null;
    try {
      serverSocket = new ServerSocket(SERVER_PORT);
      LOGGER.info("监听的端口:{} " , serverSocket.getLocalPort());
      while (true) {
        Socket socketClient = serverSocket.accept();
        LOGGER.info("客户端IP:{}" , socketClient.getInetAddress() );
        LOGGER.info("客户端端口:{}" , socketClient.getPort() );
        new LongConnectionExecute(socketClient).start();
        hashtable.put(socketClient.getInetAddress().toString(), socketClient);
      }
    } catch (Exception e) {
      LOGGER.error("connection server error:",e);
      serverSocket.close();
    }
  }

}
